package com.example.myapplication;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.Data.Scan;

import java.util.ArrayList;
import java.util.List;

public class ScanList extends AppCompatActivity {

    public List<Scan> scanList = new ArrayList<>();
    MyAdapter mMyAdapter;
    RecyclerView mRecyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_scan_list);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setBackgroundResource(R.color.blue);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        for(int i =0;i<20;i++){
            Scan scan = new Scan("androidmm", "ip","android");
            scan.hostName = "nnnn"+i;
            scanList.add(scan);
        }

        mRecyclerView = findViewById(R.id.recycleView);
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(ScanList.this);
        mRecyclerView.setLayoutManager(layoutManager);


    }

    // 作为toolbar中返回按钮的功能，实现返回上一个页面
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    class  MyViewHolder extends  RecyclerView.ViewHolder{
        TextView hostNameItem;
        TextView ipItem;
        TextView terminalTypeItem;
        public MyViewHolder(View itemView){
            super(itemView);
            hostNameItem = itemView.findViewById(R.id.hostName);
            ipItem = itemView.findViewById(R.id.ip);
            terminalTypeItem = itemView.findViewById(R.id.terminalType);

        }
    }

    class  MyAdapter extends  RecyclerView.Adapter<MyViewHolder>{

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(ScanList.this,R.layout.activity_scan_view,null);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            Scan scan = scanList.get(position);
            holder.hostNameItem.setText(scan.hostName);
            holder.hostNameItem.setText(scan.ip);
            holder.hostNameItem.setText(scan.terminalType);
        }

        @Override
        public int getItemCount() {
            return scanList.size();
        }
    }



}